package co.id.vivastor.pantau;

import co.id.vivastor.pantau.util.Global;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class SettingActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		_setOtherViews();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_setting, menu);
		return true;
	}
	
	public void _setOtherViews() {
		//Icon Clickable goto home menu:
		ImageView icon = (ImageView) findViewById(R.id.imageView1);
		Global.setImageViewButtonListener(icon, SettingActivity.this, MainActivity.class);
		
		//Isi settingan yang telah tersimpan
		EditText kodeWilayahEditText = (EditText) findViewById(R.id.editText_kode_wilayah);
		EditText nomorTPSEditText = (EditText) findViewById(R.id.editText_nomor_TPS);
		EditText nomorSMSCenterEditText = (EditText) findViewById(R.id.editText_nomor_SMS_center);
		
		kodeWilayahEditText.setText(Global.getData(getApplicationContext(), Global.KODE_WILAYAH, Global.DEFAULT_VALUE));
		nomorTPSEditText.setText(Global.getData(getApplicationContext(), Global.NOMOR_TPS, Global.DEFAULT_VALUE));
		nomorSMSCenterEditText.setText(Global.getData(getApplicationContext(), Global.SMS_CENTER_NO, Global.DEFAULT_VALUE));
	}
	
	public void btnSaveSettingOnClick(View v) {
		EditText kodeWilayahEditText = (EditText) findViewById(R.id.editText_kode_wilayah);
		EditText nomorTPSEditText = (EditText) findViewById(R.id.editText_nomor_TPS);
		EditText nomorSMSCenterEditText = (EditText) findViewById(R.id.editText_nomor_SMS_center);
		
		String kodeWilayah = kodeWilayahEditText.getText().toString();
		String nomorTPS = nomorTPSEditText.getText().toString().toString();
		String nomorSMSCenter = nomorSMSCenterEditText.getText().toString();
		if (kodeWilayah == Global.DEFAULT_VALUE || nomorTPS == Global.DEFAULT_VALUE || nomorSMSCenter == Global.DEFAULT_VALUE || 
				kodeWilayah == "" || nomorTPS == "" || nomorSMSCenter == "") {
			Toast.makeText(this, "Konfigurasi gagal. Cek isian kembali", Toast.LENGTH_SHORT).show();
		} else {
			Global.saveData(getApplicationContext(), Global.KODE_WILAYAH, kodeWilayah);
			Global.saveData(getApplicationContext(), "nomorTPS", nomorTPS);
			Global.saveData(getApplicationContext(), "SMSCenterNumber", nomorSMSCenter);

			Toast.makeText(this, "Konfigurasi berhasil tersimpan.", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(SettingActivity.this, MainActivity.class);
			this.startActivity(intent);

		}
	}

}
